package com.example.apple.paopo.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.example.apple.paopo.R;
import com.example.apple.paopo.base.BaseActivity;
import com.example.apple.paopo.dialog.PaoPoDialog;
import com.vondear.rxtools.RxNetTool;
import com.vondear.rxtools.view.RxToast;
import com.zyao89.view.zloading.ZLoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by apple on 17/12/7.
 */
@ContentView(R.layout.activity_passwordmodify)
public class PasswordModifyActivity extends BaseActivity{
    @ViewInject(R.id.passwordmodify_et_newpwd1)
    EditText newpwd1;
    @ViewInject(R.id.passwordmodify_et_newpwd2)
    EditText newpwd2;
    SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        sharedPreferences =getSharedPreferences("PAOPO",MODE_APPEND);
    }
    @Event(R.id.passwordmodify_bt_confirm)
    private void confirmAction(View v){
       // phoneModificationCipher
       // final ZLoadingDialog dialog =PaoPoDialog.getPaoPoDialog(PasswordModifyActivity.this,"正在提交PAOPO信息,请稍后...");
        if(!RxNetTool.isNetworkAvailable(this)){
            RxToast.error(this,"发送失败，请检查网络是否可用").show();
            return;
        }
        if (newpwd1.getText().toString().equals(newpwd2.getText().toString())) {
            final ZLoadingDialog dialog = PaoPoDialog.getPaoPoDialog(PasswordModifyActivity.this, "正在提交PAOPO信息,请稍后...");
            RequestParams params = new RequestParams("http://cloud.bmob.cn/91c9d2dffacfcd37/phoneModificationCipher");
            params.addQueryStringParameter("token", sharedPreferences.getString("phonepwd", ""));
            params.addQueryStringParameter("newpwd",newpwd1.getText().toString());
            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.getString("sate").equals("YES")){
                            RxToast.success(PasswordModifyActivity.this,"修改成功").show();
                        }else {
                            RxToast.error(PasswordModifyActivity.this,"修改失败").show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    dialog.dismiss();
                    finish();
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    RxToast.error(PasswordModifyActivity.this,"修改失败").show();
                    finish();
                    dialog.dismiss();
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    dialog.dismiss();
                }

                @Override
                public void onFinished() {
                    dialog.dismiss();
                }
            });
        }else {
            RxToast.error(this,"两次密码不相同，请重新输入").show();
            return;
        }
    }
}
